# Values set at CMake configure time
set(CBDIR "@CMAKE_CURRENT_BINARY_DIR@")
set(CTRLIMG "@CONTROL_IMAGE@")
set(LOGFILE "@LOG_FILE@")
set(SRCIMG "@INPUT_IMAGE@")
set(TGT "@TARGET_NAME@")
set(TGTIMG "@TARGET_IMAGE@")

file(WRITE "${LOGFILE}" "Starting icv_export run\n")

# The executable locations aren't know at CMake configure time, so one of them
# is passed in via the EXEC variable at runtime.  De-quote it and assign it to
# the appropriate variable.
string(REPLACE "\\" "" ICV_EXEC "${EXEC}")
if(NOT EXISTS "${ICV_EXEC}")
  file(WRITE "${LOGFILE}" "icv not found at location \"${ICV_EXEC}\" - aborting\n")
  file(READ "${LOGFILE}" LOG)
  message(FATAL_ERROR "Unable to find icv, aborting.\nSee ${LOGFILE} for more details.\n${LOG}")
endif(NOT EXISTS "${ICV_EXEC}")

# Apparently CMAKE_EXECUTABLE_SUFFIX is not reliably defined in cmake -P - get
# it from the executable passed in to us.
get_filename_component(EXE_SUFFIX "${ICV_EXEC}" EXT)

# Use the path passed in from the build target generator expression
# to ascertain the current binary directory in use, and locate any
# other necessary programs.
get_filename_component(BDIR "${ICV_EXEC}" DIRECTORY)
set(PIXCMP_EXEC "${BDIR}/pixcmp${EXE_SUFFIX}")
if(NOT EXISTS "${PIXCMP_EXEC}")
  file(WRITE "${LOGFILE}" "pixcmp not found at location \"${PIXCMP_EXEC}\" - aborting\n")
  file(READ "${LOGFILE}" LOG)
  message(FATAL_ERROR "Unable to find pixcmp, aborting.\nSee ${LOGFILE} for more details.\n${LOG}")
endif(NOT EXISTS "${PIXCMP_EXEC}")

# Clean up in case we've run before unsuccessfully
set(OUTPUT_FILES ${TGTIMG} ${CBDIR}/${TGT}-ctrl.pix ${CBDIR}/${TGT}-out.pix)
foreach(of ${OUTPUT_FILES})
  execute_process(
    COMMAND "@CMAKE_COMMAND@" -E remove -f "${of}"
  )
endforeach(of ${OUTPUT_FILES})

# Perform the ICV conversion
file(APPEND "${LOGFILE}" "Converting ${SRCIMG} to ${TGTIMG}\n")
execute_process(
  COMMAND "${ICV_EXEC}" "${SRCIMG}" "${TGTIMG}"
  RESULT_VARIABLE icv_result
  OUTPUT_VARIABLE icv_log
  ERROR_VARIABLE icv_log
)
file(APPEND "${LOGFILE}" "${icv_log}")
set(icv_log)
if(NOT EXISTS "${TGTIMG}")
  file(APPEND "${LOGFILE}" "Failure: ${icv_result}")
  file(READ "${LOGFILE}" LOG)
  message(
    FATAL_ERROR
    "Unable to convert ${SRCIMG} to ${TGTIMG} with ${ICV_EXEC}, aborting.\nSee ${LOGFILE} for more details.\n${LOG}"
  )
endif(NOT EXISTS "${TGTIMG}")

# Because we are exporting, we need to convert both the control image
# and the target image to PIX format for pixcmp to do its check.
file(APPEND "${LOGFILE}" "Converting ${CTRLIMG} to ${CBDIR}/${TGT}-ctrl.pix\n")
execute_process(
  COMMAND "${ICV_EXEC}" "${CTRLIMG}" "${CBDIR}/${TGT}-ctrl.pix"
  RESULT_VARIABLE icv_result
  OUTPUT_VARIABLE icv_log
  ERROR_VARIABLE icv_log
)
file(APPEND "${LOGFILE}" "${icv_log}")
set(icv_log)
if(NOT EXISTS "${CBDIR}/${TGT}-ctrl.pix")
  file(APPEND "${LOGFILE}" "Failure: ${icv_result}")
  file(READ "${LOGFILE}" LOG)
  message(
    FATAL_ERROR
    "Unable to convert ${SRCIMG} to ${CBDIR}/${TGT}-ctrl.pix with ${ICV_EXEC}, aborting.\nSee ${LOGFILE} for more details.\n${LOG}"
  )
endif(NOT EXISTS "${CBDIR}/${TGT}-ctrl.pix")

file(APPEND "${LOGFILE}" "Converting ${TGTIMG} to ${CBDIR}/${TGT}-out.pix\n")
execute_process(
  COMMAND "${ICV_EXEC}" "${TGTIMG}" "${CBDIR}/${TGT}-out.pix"
  RESULT_VARIABLE icv_result
  OUTPUT_VARIABLE icv_log
  ERROR_VARIABLE icv_log
)
file(APPEND "${LOGFILE}" "${icv_log}")
set(icv_log)
if(NOT EXISTS "${CBDIR}/${TGT}-out.pix")
  file(APPEND "${LOGFILE}" "Failure: ${icv_result}")
  file(READ "${LOGFILE}" LOG)
  message(
    FATAL_ERROR
    "Unable to convert ${TGTIMG} to ${CBDIR}/${TGT}-out.pix with ${ICV_EXEC}, aborting.\nSee ${LOGFILE} for more details.\n${LOG}"
  )
endif(NOT EXISTS "${CBDIR}/${TGT}-out.pix")

# pixcmp the results with the control image to make sure icv got it right
file(APPEND "${LOGFILE}" "\nComparing ${CBDIR}/${TGT}-ctrl.pix to ${CBDIR}/${TGT}-out.pix:\n")
execute_process(
  COMMAND "${PIXCMP_EXEC}" "${CMAKE_CURRENT_BINARY_DIR}/${TGT}-ctrl.pix" "${CMAKE_CURRENT_BINARY_DIR}/${TGT}-out.pix"
  RESULT_VARIABLE pixcmp_val
  OUTPUT_VARIABLE icv_log
  ERROR_VARIABLE icv_log
)
file(APPEND "${LOGFILE}" "${icv_log}")

# Final success/failure check
if(${pixcmp_val} GREATER 0)
  file(APPEND "${LOGFILE}" "Failure: ${pixcmp_val}")
  file(READ "${LOGFILE}" LOG)
  message(
    FATAL_ERROR
    "Differences found between ${CBDIR}/${TGT}-ctrl.pix and ${CBDIR}/${TGT}-out.pix with ${PIXCMP}, aborting.\nSee ${LOGFILE} for more details.\n${LOG}"
  )
else(${pixcmp_val} GREATER 0)
  foreach(of ${OUTPUT_FILES})
    execute_process(
      COMMAND "@CMAKE_COMMAND@" -E remove -f "${of}"
    )
  endforeach(of ${OUTPUT_FILES})
endif(${pixcmp_val} GREATER 0)

# Local Variables:
# tab-width: 8
# mode: cmake
# indent-tabs-mode: t
# End:
# ex: shiftwidth=2 tabstop=8
